$(document).ready(function () {
    addChartContent();
});
function addChartContent() {
    let monitorIdinURLstr = window.location.search
    // 获取monitorId
    // let monitorIdinURL= monitorIdinURLstr.slice(11)
    let monitorStart = monitorIdinURLstr.indexOf('=')
    let monitorEnd = monitorIdinURLstr.indexOf('&')
    let monitorIdinURL= monitorIdinURLstr.slice(monitorStart+1,monitorEnd)
    // 获取pagesize参数
    let pagesizeStr = monitorIdinURLstr.slice(monitorEnd)
    let pagesizeinURL= pagesizeStr.slice(10)
            $.ajax({
                url:`http://47.116.135.187:8083/busi/monitor/report?monitorId=${monitorIdinURL}`,
                dataType: "json",
                success: function(data) {
                    // 表头
                    var reportData = data.data
                    var dataArray = topData(reportData['project'],reportData['equipment'],reportData['lastMonitor'],reportData['monitor']);

                    //添加抬头
                    dataArray.push(['测点编号', '初始高程（m）','上期高程（m）', '本期高程（m）','变化量(mm)', null,'变化速率(mm/d)', null,'设计限值',null, '预警等级','备注' ]);
                    dataArray.push([null, null,null,null,'本期变化量', '累计变化量','日变化速率', '累计变化速率','变化速率','限值(mm)', null,null ]);
                    var mptypearr = []
                    // 表格数据
                    reportData['altitudesCurrentList'].forEach((item,index) => {
                        var arr =[]
                        mptypearr.push(item['mpType'])
                })

                    // mptype去重，得到没有重复值的mptype合集。
                    var mptypearrnorepeat = Array.from(new Set(mptypearr))
                    var mpgroup =new Array();
                    var mpgroupFourTitle =new Array();
                    // 划分key为mptype的map数据结构
                    // var arr1 = []
                    let mptypeMap = new Map();
                    mptypearrnorepeat.forEach(value=>{
                      mptypeMap.set(value,0);
                    })

                    var groupData = new Map();

                    var currentArray = [];
                    var m=0;
                    var pageSize=pagesizeinURL;
                    //最后一个不算
                    for(var i=0;i<reportData['altitudesCurrentList'].length;i++){
                        m++;
                        var item = reportData['altitudesCurrentList'][i];

                        var arr=[];
                        arr.push(item['mpNo'])
                        arr.push(item['baseAltitude'])
                        arr.push(item['preAltitude'])
                        arr.push(item['targetCorrectAutitude'])
                        arr.push(item['currentChange'])
                        arr.push(item['sumChange'])
                        arr.push(item['dailyChangeRate'])
                        arr.push(item['sumChangeRate'])
                        if(item['minChangeRate']+item['maxChangeRate']==0){
                            arr.push(`±${item['maxChangeRate']}`)
                        }
                        else{
                            arr.push(`${item['minChangeRate']}~${item['maxChangeRate']}`)
                        }
                        if(item['minSumChange']+item['maxSumChange']==0){
                            arr.push(`±${item['maxSumChange']}`)
                        }
                        else{
                            arr.push(`${item['minSumChange']}~${item['maxSumChange']}`)
                        }

                        arr.push(item['warningLevel'])
                        arr.push(null);
                        currentArray.push(arr);
                        if(i==reportData['altitudesCurrentList'].length-1){
                            var gsize = Number(mptypeMap.get(item['mpType']))+1;
                            groupData.set(item['mpType']+gsize,currentArray);
                            mptypeMap.set(item['mpType'],gsize);
                            currentArray=[];
                            m=0;
                            pageSize=20;
                            mpgroup.push(item['mpType']+gsize);
                            mpgroupFourTitle.push(item['mpType']);
                        }
                        else{
                            var itemNext = reportData['altitudesCurrentList'][i+1];
                            if(item['mpType']!=itemNext['mpType']){
                                var gsize = Number(mptypeMap.get(item['mpType']))+1;
                                groupData.set(item['mpType']+gsize,currentArray);
                                mptypeMap.set(item['mpType'],gsize);
                                currentArray=[];
                                m=0;
                                pageSize=20;
                                mpgroup.push(item['mpType']+gsize);
                                mpgroupFourTitle.push(item['mpType']);
                            }
                            else if(item['mpType']==itemNext['mpType']&&m==pageSize&&item['mpNo'].split("-")[0]!=itemNext['mpNo'].split("-")[0]){
                                var gsize = Number(mptypeMap.get(item['mpType']))+1;
                                groupData.set(item['mpType']+gsize,currentArray);
                                mptypeMap.set(item['mpType'],gsize);
                                currentArray=[];
                                m=0;
                                pageSize=20;
                                mpgroup.push(item['mpType']+gsize);
                                mpgroupFourTitle.push(item['mpType']);
                            }
                            else if(item['mpType']==itemNext['mpType']&&m==pageSize&&item['mpNo'].split("-")[0]==itemNext['mpNo'].split("-")[0]){
                                pageSize=pageSize-1;
                                currentArray.pop();//当前塞入的去除
                                currentArray.pop();//上轮塞入的去除
                                m=m-2;
                                i=i-2;  //回到上一个I
                            }

                        }
                    }

                    // 分页
                    for(var i=0;i<mpgroup.length;i++){
                        let typeDataArr = [];
                        let pagesdataarr = [];
                        pagesdataarr=groupData.get(mpgroup[i])
                        typeDataArr.push(...dataArray);
                        typeDataArr.push(...pagesdataarr);
                        // 第四行监测报表类别
                        typeDataArr[3][0]=mpgroupFourTitle[i]+"监测报表"
                        let mpNoMap = new Map();
                        pagesdataarr.forEach(item=>{
                            mpNoMap.set(item[0],item[0]);
                       })
                       // 设置图表所需的空白区域
                       var arrForPlaceOfChart = [
                           [null],
                           [null],
                           [null],
                           [null],
                           [null],
                           [null],
                           [null],
                           [null],
                           [null],
                           [null],
                           [null],
                           [null],
                           [null]
                       ]
                       typeDataArr.push(...arrForPlaceOfChart);
                       typeDataArr.push(...bottomData());
                        const mapArr = new Map()
                        var mpnoArr = []
                        var itemofitemArr = []
                        reportData.chartGroupList.forEach(item=>{
                             var mpNo = item['mpNo'];
                             if(mpNoMap.has(mpNo)){
                                item['dataList'].forEach(itemofitem => {
                                    itemofitemArr.push(itemofitem.value)
                                    
                                })
                                mapArr.set(item['mpNo'],itemofitemArr)
                                itemofitemArr=[];
                                mpnoArr.push(item['mpNo'])
                             }
                            
                        })
                        typeDataArr.push([null,...reportData.dateArr])
                        mpnoArr.forEach(v=>{
                            typeDataArr.push([v,...mapArr.get(v)])
                        })
                        var sheet = new spreadNS.Worksheet(mpgroup[i]);
                        sheet.setColumnCount(50);
                        sheet.setArray(0, 0, typeDataArr);
                        // 设置行数和列数
                        sheet.setRowCount(600)
                        // 自动换行
                       sheet.getCell(6, 0).wordWrap(true);
                       sheet.getCell(6, 1).wordWrap(true);
                       sheet.getCell(6, 2).wordWrap(true);
                       sheet.getCell(6, 3).wordWrap(true);
                        // 用函数设置表头
                        sheet.addSpan(0, 0, 1, 12);
                        sheet.getCell(0, 0).font("20px Arial").hAlign(spreadNS.HorizontalAlign.center).vAlign(spreadNS.VerticalAlign.center);
                        // 获取列宽、行高
                        var columnWidth = sheet.getColumnWidth(1);
                        var rowHeight = sheet.getRowHeight(1);
                        // 设置表头边框，前4行不要边框
                        sheet.getRange(4, 0, 8, 12).setBorder(new spreadNS.LineBorder("black", spreadNS.LineStyle.thin), {all: true});
                        sheet.addSpan(1, 0, 1, 5);
                        sheet.addSpan(2, 0, 1, 5);
                        sheet.addSpan(1, 8, 1, 4);
                        sheet.addSpan(2, 8, 1, 4);
                        sheet.addSpan(3, 0, 1, 11);
                        sheet.getCell(3, 0).font("20px Arial").hAlign(spreadNS.HorizontalAlign.center).vAlign(spreadNS.VerticalAlign.center);
                        sheet.getCell(3, 11).font("16px Arial").hAlign(spreadNS.HorizontalAlign.center).vAlign(spreadNS.VerticalAlign.center);
                        sheet.addSpan(4, 0, 1, 2);
                        sheet.getCell(4, 0).hAlign(spreadNS.HorizontalAlign.center).vAlign(spreadNS.VerticalAlign.center);
                        sheet.addSpan(4, 2, 1, 2);
                        sheet.getCell(4, 2).hAlign(spreadNS.HorizontalAlign.center).vAlign(spreadNS.VerticalAlign.center);
                        sheet.addSpan(4, 4, 1, 2);
                        sheet.getCell(4, 4).hAlign(spreadNS.HorizontalAlign.center).vAlign(spreadNS.VerticalAlign.center);
                        sheet.addSpan(4, 6, 1, 2);
                        sheet.getCell(4, 6).hAlign(spreadNS.HorizontalAlign.center).vAlign(spreadNS.VerticalAlign.center);
                        sheet.getCell(4, 8).hAlign(spreadNS.HorizontalAlign.center).vAlign(spreadNS.VerticalAlign.center);
                        sheet.addSpan(4, 9, 1, 2);
                        sheet.getCell(4, 9).hAlign(spreadNS.HorizontalAlign.center).vAlign(spreadNS.VerticalAlign.center);
                        sheet.addSpan(5, 0, 1, 2);
                        sheet.getCell(5, 0).hAlign(spreadNS.HorizontalAlign.center).vAlign(spreadNS.VerticalAlign.center);
                        sheet.addSpan(5, 2, 1, 2);
                        sheet.getCell(5, 2).hAlign(spreadNS.HorizontalAlign.center).vAlign(spreadNS.VerticalAlign.center);
                        sheet.addSpan(5, 4, 1, 2);
                        sheet.getCell(5, 4).hAlign(spreadNS.HorizontalAlign.center).vAlign(spreadNS.VerticalAlign.center);
                        sheet.addSpan(5, 6, 1, 2);
                        sheet.getCell(5, 6).hAlign(spreadNS.HorizontalAlign.center).vAlign(spreadNS.VerticalAlign.center);
                        sheet.getCell(5, 8).hAlign(spreadNS.HorizontalAlign.center).vAlign(spreadNS.VerticalAlign.center);
                        sheet.getCell(5, 9).hAlign(spreadNS.HorizontalAlign.center).vAlign(spreadNS.VerticalAlign.center);
                        sheet.getCell(5, 10).hAlign(spreadNS.HorizontalAlign.center).vAlign(spreadNS.VerticalAlign.center);
                        sheet.getCell(5, 11).hAlign(spreadNS.HorizontalAlign.center).vAlign(spreadNS.VerticalAlign.center);
                        // 测点编号-本期高程
                        sheet.addSpan(6, 0, 2, 1);
                        sheet.getCell(6, 0).hAlign(spreadNS.HorizontalAlign.center).vAlign(spreadNS.VerticalAlign.center);
                        sheet.getCell(6, 1).hAlign(spreadNS.HorizontalAlign.center).vAlign(spreadNS.VerticalAlign.center);
                        sheet.getCell(6, 2).hAlign(spreadNS.HorizontalAlign.center).vAlign(spreadNS.VerticalAlign.center);
                        sheet.getCell(6, 3).hAlign(spreadNS.HorizontalAlign.center).vAlign(spreadNS.VerticalAlign.center);
                        sheet.addSpan(6, 1, 2, 1);
                        sheet.addSpan(6, 2, 2, 1);
                        sheet.addSpan(6, 3, 2, 1);
                        // 变化量、变化速率、设计限值
                        sheet.getCell(6, 4).hAlign(spreadNS.HorizontalAlign.center).vAlign(spreadNS.VerticalAlign.center);
                        sheet.getCell(6, 6).hAlign(spreadNS.HorizontalAlign.center).vAlign(spreadNS.VerticalAlign.center);
                        sheet.getCell(6, 8).hAlign(spreadNS.HorizontalAlign.center).vAlign(spreadNS.VerticalAlign.center);
                        sheet.addSpan(6, 4, 1, 2);
                        sheet.addSpan(6, 6, 1, 2);
                        sheet.addSpan(6, 8, 1, 2);
                        // 预警等级、备注
                        sheet.getCell(6, 10).hAlign(spreadNS.HorizontalAlign.center).vAlign(spreadNS.VerticalAlign.center);
                        sheet.getCell(6, 11).hAlign(spreadNS.HorizontalAlign.center).vAlign(spreadNS.VerticalAlign.center);
                        sheet.addSpan(6, 10, 2, 1);
                        sheet.addSpan(6, 11, 2, 1);
                        // 本期变化量-限值mm居中
                        sheet.getCell(7, 4).hAlign(spreadNS.HorizontalAlign.center).vAlign(spreadNS.VerticalAlign.center);
                        sheet.getCell(7, 5).hAlign(spreadNS.HorizontalAlign.center).vAlign(spreadNS.VerticalAlign.center);
                        sheet.getCell(7, 6).hAlign(spreadNS.HorizontalAlign.center).vAlign(spreadNS.VerticalAlign.center);
                        sheet.getCell(7, 7).hAlign(spreadNS.HorizontalAlign.center).vAlign(spreadNS.VerticalAlign.center);
                        sheet.getCell(7, 8).hAlign(spreadNS.HorizontalAlign.center).vAlign(spreadNS.VerticalAlign.center);
                        sheet.getCell(7, 9).hAlign(spreadNS.HorizontalAlign.center).vAlign(spreadNS.VerticalAlign.center);

                        sheet.suspendPaint();
                        

                        spread.addSheet(spread.getSheetCount()-1, sheet);

                        // 表格行数（数组长度）
                        let arrlength = pagesdataarr.length
                        // 表尾边框
                        sheet.getRange(8, 0, arrlength+15 , 12).setBorder(new spreadNS.LineBorder("black", spreadNS.LineStyle.thin), {all: true});

                        // 合并图表区域背景单元格
                        sheet.addSpan(arrlength+8, 0, 13, 12);

                        // 表尾合并单元格
                        sheet.addSpan(arrlength+21, 0, 1, 2);
                        sheet.addSpan(arrlength+21, 2, 1, 10);
                        sheet.addSpan(arrlength+22, 0, 1, 2);
                        sheet.addSpan(arrlength+22, 2, 1, 2);
                        sheet.addSpan(arrlength+22, 4, 1, 2);
                        sheet.addSpan(arrlength+22, 6, 1, 2);
                        sheet.addSpan(arrlength+22, 8, 1, 2);
                        sheet.addSpan(arrlength+22, 10, 1, 2)
                        // 部分单元格自动适应列宽
                        sheet.autoFitColumn(4)
                        sheet.autoFitColumn(5)
                        sheet.autoFitColumn(6)
                        sheet.autoFitColumn(7)
                        sheet.autoFitColumn(8)
                        sheet.autoFitColumn(9)


                        // 生成图表
                        let str = "A"+Number(Number(arrlength)+Number(24))+":AF"+Number(Number(arrlength)+Number(24)+Number(mpnoArr.length))
                        var chart = sheet.charts.add('line', GC.Spread.Sheets.Charts.ChartType.lineMarkers, 1, 8*rowHeight+pagesdataarr.length*rowHeight, 12*columnWidth, 250, str)
                        // 设置图表标题
                        var title = chart.title();
                        title.text = mpgroupFourTitle[i];
                        chart.title(title);

                        // //打印
                        // var printInfo = sheet.printInfo();
                        // printInfo.rowEnd(arrlength+22);
                        // printInfo.columnEnd(12)
                        // sheet.printInfo(printInfo)
                        // console.log(printInfo,'printInfo');

                        var legend = chart.legend();
                        legend.visible = true;
                        chart.legend(legend);
                        
                        sheet.resumePaint();

                    }
                    
                }
            })


    
}
